package com.liupf.common.utils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;



import cn.jpush.api.JPushClient;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

public class PushTokenUtils {
	
	private static final String key = "360f07bb83d32e04fd5fdcb4";
	private static final String pass = "a8fe8c492dc31956004e6116";//AppKey:a8fe8c492dc31956004e6116 360f07bb83d32e04fd5fdcb4
	private static JPushClient jPushClient = new JPushClient(key, pass, 3);
	
	public static enum JpushType{
    	order_driver_accept,
    	driver_balance_change,
    	sys;
    	
    	public String getReferType() {
            return this.name().toLowerCase().replaceAll("_", ":");
        }
    }

	private static void sendPush(Map<String,String> extrasMap,
    	    String alert,String title,String alias) {
	    
		PushPayload.Builder pushPayloadBuilder = new PushPayload.Builder();
		
		pushPayloadBuilder.setPlatform(Platform.all());
        pushPayloadBuilder.setNotification(Notification.newBuilder()
                .addPlatformNotification(IosNotification.newBuilder().setAlert(alert).addExtras(extrasMap).setSound("default").build())
                .addPlatformNotification(AndroidNotification.newBuilder().setAlert(alert).setTitle(alert).addExtras(extrasMap).build()).build());
        Options options = Options.sendno();
        options.setApnsProduction(true);
        pushPayloadBuilder.setAudience(Audience.alias(alias));

        try {
            pushPayloadBuilder.setOptions(options);
            jPushClient.sendPush(pushPayloadBuilder.build());
        } catch (Exception e) {
        	e.printStackTrace();
        }
    }
	
	public static void sendPush(String alert,String alias, JpushType jpushType, Map<String, String> paramsMap){
		Map<String, String> allMap = new HashMap<String, String>();
		allMap.put("t", jpushType.getReferType());
		if(paramsMap != null){
			allMap.putAll(paramsMap);
		}
		sendPush(allMap, alert, alert, alias);
	}
	
}
